Newer
Older
TheVengeance-Project-IADE-Unity2D / Assets / Scripts / NPC / Quest NPC / QuestNPCMovement.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class QuestNPCMovement : MonoBehaviour
{
    private Rigidbody2D rb;
    private RangedArea rangedArea;

    [SerializeField] private float moveForce = 1f;
    private Vector3[] positionArray;
    private int pointsIndex;

    // Start is called before the first frame update
    void Start()
    {
        rangedArea = FindObjectOfType<RangedArea>();
        rb = GetComponent<Rigidbody2D>();
        positionArray = new[] {new Vector3(6f, -10.5f), new Vector3(18.5f, -10.5f),
                                new Vector3(18.5f, 8.5f)};

        pointsIndex = 0;
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        if (rangedArea.playerInRange == false)
        {
            transform.position = Vector3.MoveTowards(transform.position, positionArray[pointsIndex], moveForce * Time.deltaTime);

            if (transform.position == (positionArray[pointsIndex]))
            {
                //Next point of the array of Locations
                pointsIndex++;
            }

            if (pointsIndex == (positionArray.Length))
            {
                //Going Back to the start point
                pointsIndex = 0;
            }

        }
    }
}